package com.wunu.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import com.wunu.entity.CmdIssued;
import com.wunu.entity.ImageMongo;
import com.wunu.mapper.LockMemberMapper;
import com.wunu.service.ICmdIssuedMongodbService;
import com.wunu.util.DateTimeUtil;
import com.wunu.vo.UserCmdIssuedVo;

@Component
public class CmdIssuedMgDbServiceImpl implements ICmdIssuedMongodbService {
	@Resource
	private MongoTemplate mongoTemplate;
	@Autowired
	private LockMemberMapper lockMemberMapper;
	@Override
	public Map<String, Object> getSendCmd(Integer pageIndex,Integer pageSize ,String clientId) {
		 List<UserCmdIssuedVo> userCmdIssueds = new ArrayList<UserCmdIssuedVo>();
			Map<String, Object> resultMap = new HashMap<>();
			//当前页码 默认给了0
		    pageSize = pageSize!=0?pageSize:10;
		    //页面展示数据条数 默认给了 10
		    int currentPage =pageIndex!=0?pageIndex:1;
		    //筛选条件
		    Query query = new Query().addCriteria(Criteria.where("clientId").is(clientId).and("endTime").gte(DateTimeUtil.nowDateTime()));
		    // 设置起始数 	// 设置查询条数
		    query.skip((currentPage - 1) * pageSize).limit(pageSize);
		    long totalCount = mongoTemplate.count(query,ImageMongo.class);
			// 数据总页数
			int totalPage = (int) (totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1);
			// 设置记录总数和总页数
			resultMap.put("totalCount", totalCount);
			resultMap.put("totalPage", totalPage);
			// 按创建时间倒序
			query.with(new Sort(Sort.Direction.DESC, "createTime"));
			// 查询当前页数据集合
			 List<Map<String, Object>> members = lockMemberMapper.selectFlockMembers(clientId);
			 List<CmdIssued> cmdIssueds = mongoTemplate.find(query, CmdIssued.class);
			 for(CmdIssued cmdIssued :cmdIssueds) {
				 for(Map<String,Object> map : members) {
					 if(!map.get("userId").equals(cmdIssued.getUserId())) continue;
					 UserCmdIssuedVo userCmdIssuedVo = new UserCmdIssuedVo();
					 userCmdIssuedVo.setAvatar(map.get("avatar")!=null?map.get("avatar").toString():"");
					 userCmdIssuedVo.setNickName(StringUtils.isNotBlank(cmdIssued.getRemark())?cmdIssued.getRemark():map.get("nickName").toString());
					 userCmdIssuedVo.setPitNum(cmdIssued.getPitNum());
					 userCmdIssuedVo.setUserId(cmdIssued.getUserId());
					 userCmdIssuedVo.setEndTime(cmdIssued.getEndTime());
					 userCmdIssuedVo.setStartTime(cmdIssued.getStartTime());
					 userCmdIssuedVo.setData(cmdIssued.getData());
					 userCmdIssuedVo.setValid(cmdIssued.isValid());
					 userCmdIssuedVo.setSucc(cmdIssued.isSucc());
					 userCmdIssuedVo.setType(cmdIssued.getType());
					 if((cmdIssued.isValid()&&cmdIssued.isSucc())||cmdIssued.getStatus()==1) {
						 userCmdIssuedVo.setStatus(1);
					 }else if((cmdIssued.isValid()&&!cmdIssued.isSucc())||cmdIssued.getStatus()==0) {
						 userCmdIssuedVo.setStatus(0);
					 }else if((!cmdIssued.isValid()||cmdIssued.isSucc())||cmdIssued.getStatus()==3) {
						 userCmdIssuedVo.setStatus(3);
					 }
					 userCmdIssuedVo.setDisable(map.get("role").equals(1)?false:true);
					 userCmdIssuedVo.setAdminPwd(false);
					 userCmdIssueds.add(userCmdIssuedVo);
				 }
			 }
		    resultMap.put("content", userCmdIssueds);
			return resultMap;
	}
	
}
